using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb;
using Decav.Gatsb.Application;
using Decav.Gatsb.Data;
using Decav.Gatsb.Web;
using System.Collections.Generic;

public partial class Person : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GatsbWebApplication.SetPageName(this, GatsbUser.Name.ToString());
        SetViewPermissions();
        SetRelationshipStatus();
        SetupEvents();
        SetCurrentLocation();

        aboutTextLiteral.Text = StringFormatting.PlainTextToHtml( UserProfile.About );
        profileImage.ImageId = GatsbUser.Id;

        profileImage.Alt = GatsbUser.Name.ToString();
    }

    private void SetCurrentLocation()
    {
        if (User.Identity.IsAuthenticated && GatsbUser.Id == GatsbApplication.Current.CurrentUser.Id)
        {
            setMyLocation.Visible = true;
            currentLocationSpan.Visible = false;
            noLocationSpan.Visible = false;
        }
        else
        {
            currentLocationSpan.Visible = false;
            noLocationSpan.Visible = false;
            setMyLocation.Visible = false;

            LocationCheckin location = null;
            try
            {
                 location = GatsbApplication.Current.GetUserLocation(GatsbUser);
            }
            catch (GatsbDataException)
            {
                return;
            }

            if (location.LocationSpecified)
            {
                currentLocationSpan.Visible = true;
                locationLink.Location = location.Location;
            }
            else
            {
                noLocationSpan.Visible = true;
            }
        }
    }

    private void SetupEvents()
    {
        Decav.Gatsb.Post[] posts = null;
        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            posts = adapt.GetLatestUserPosts(UserId, 50);
        }

        if (posts.Length > 0)
        {
            eventsRepeater.Visible = true;
            noEventsDiv.Visible = false;
            eventsRepeater.DataSource = posts;
            eventsRepeater.DataBind();
        }
        else
        {
            eventsRepeater.Visible = false;
            noEventsDiv.Visible = true;

            if (User.Identity.IsAuthenticated && GatsbUser.Id == GatsbApplication.Current.CurrentUser.Id)
                noEventsDiv.InnerHtml = "You haven't posted anything to Gatsb yet!<br/>Read the <a href=\"GettingStarted.aspx\">Getting Started Guide</a> to learn to post to Gatsb!";
            else
                noEventsDiv.InnerHtml = string.Format("{0} has not shared any public events with Gatsb.", GatsbUser.Name.FirstName);
        }

    }

    private void SetViewPermissions()
    {
        if (GatsbUser.RequestingUserCanSeeProfile)
        {
            profilePage.Visible = true;
            noPermissionsProfilePage.Visible = false;
        }
        else
        {
            noPermissionsTile.User = GatsbUser;
            profilePage.Visible = false;
            noPermissionsProfilePage.Visible = true;
        }

        if (User.Identity.IsAuthenticated)
        {
            noPermissionsStatusDiv.InnerText = string.Format("You do not have permission for view {0}'s profile.",
                GatsbUser.Name.FirstName);
            personMenu.Visible = true;
            noPersonMenuDiv.Visible = false;
        }
        else
        {
            noPermissionsStatusDiv.InnerHtml = string.Format("You need to <a href=\"Login.aspx?ReturnUrl={0}\">login</a> to see {1}'s profile.",
                Server.UrlEncode(Request.RawUrl),
                GatsbUser.Name.FirstName);
            noPermissionsStatusDiv.Visible = true;
            personMenu.Visible = false;
            noPersonMenuDiv.Visible = true;
        }
    }

    protected virtual void LoginLink_Click(object sender, EventArgs e)
    {
        FormsAuthentication.RedirectToLoginPage();
    }

    private void SetRelationshipStatus()
    {
        relationshipStatusDiv.Visible = false;
        if (User.Identity.IsAuthenticated && GatsbUser.Id == GatsbApplication.Current.CurrentUser.Id)
        {
            ((IMasterPageTemplate)Master).SelectedPage = MainMenuNavigationButton.MyProfile;
            relationshipStatusDiv.Visible = true;
            relationshipStatusDiv.InnerHtml = "<a href=\"EditProfile.aspx\">Edit your profile, privacy, and other settings.</a>";
        }
        else if (GatsbUser.FriendsWithRequester)
        {
            relationshipStatusDiv.Visible = true;
            relationshipStatusDiv.InnerText = string.Format("You are friends with {0}.", GatsbUser.Name.FirstName);
        }
    }

    private Decav.Gatsb.User _user;
    private UserProfile _userProfile;

    /// <summary>
    /// Gets the unique identifier of the user to view.
    /// </summary>
    public Guid UserId
    {
        get
        {
            if (Request.QueryString["id"] == null)
                return GatsbApplication.Current.CurrentUser.Id;

            return new Guid(Request.QueryString["id"]);
        }
    }

    public string DisplayName
    {
        get
        {
            if (GatsbApplication.Current.UserPresent && GatsbUser.Id == GatsbApplication.Current.CurrentUser.Id)
                return "Your";
            else
                return string.Format("{0}'s", GatsbUser.Name.FirstName);
        }
    }

    /// <summary>
    /// Gets the user this profile page is for.
    /// </summary>
    public Decav.Gatsb.User GatsbUser
    {
        get
        {
            if (_user == null)
            {
                using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
                    _user = adapt.GetUser(UserId);
            }

            return _user;
        }
    }

    /// <summary>
    /// Gets the users profile details.
    /// </summary>
    public UserProfile UserProfile
    {
        get
        {
            if (_userProfile == null)
            {
                using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
                    _userProfile = adapt.GetUserProfile(GatsbUser.Id);
            }

            return _userProfile;
        }
    }
}
